<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Query Editor</title>
</head>
<script type="text/javascript">
window.getXmlHttpReq = function() {
	var xhr = null;

	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xhr = new ActiveXObject("Microsoft.XMLHTTP");
		}
	} else {
		alert("XMLHTTPRequest not supported ...");
		xhr = false;
	}

	return xhr;
};

var submit_action = function() {
	/*var req = getXmlHttpReq();
	req.onreadystatechange = function() {
		if (req.readyState === 4) {
			if (req.status === 200) {
				asrsHandler(req.responseText);
			} else {
				alert('Request error !');
			}
		};
	}
	req.open('GET', url, true);
	req.send(null);*/

	var type = encodeURIComponent(document.getElementById("rs").checked ? "rs" : "as");

	if (type == "as") {
		var asFunc = encodeURIComponent(document.getElementById("asFunc").value);
		var precision = encodeURIComponent(document.getElementById("precision").value);
		var request = encodeURIComponent(document.getElementById("request").value);
		
		var params = "type=" + type + "&asFunc=" + asFunc + "&precision=" + precision + "&request=" + request;
	} else {
		var rsFunc = encodeURIComponent(document.getElementById("rsFunc").value);
		var path = encodeURIComponent(document.getElementById("path").value);
		var param1 = encodeURIComponent(document.getElementById("param1").value);
		var param2 = encodeURIComponent(document.getElementById("param2").value);
		var param3 = encodeURIComponent(document.getElementById("param3").value);
		var param4 = encodeURIComponent(document.getElementById("param4").value);
		var param5 = encodeURIComponent(document.getElementById("param5").value);

		var params = "type=" + type + "&rsFunc=" + rsFunc + "&path=" + path + "&param1=" + param1 + 
		"&param2=" + param2  + "&param3=" + param3 + "&param4=" + param4 + "&param5=" + param5;
	}

	var url = "../QueryEditorServlet?" + params;
	window.open(url, "QueryEditor","location=no, menubar=no, status=no, scrollbars=yes, menubar=no");
}

var send = function() {
	submit_action();
	/*if ((document.getElementById("as").checked && document.getElementById("request").value) || 
				(document.getElementById("rs").checked && document.getElementById("path").value))
		document.forms[0].submit();
	else
		alert("Empty field(s) !")*/
}

var asrsHandler = function(tableString) {
	var crossTable = eval('(' + tableString + ')');
	var rHeaderSet = crossTable.rowHeaderSet;
	var cHeaderSet = crossTable.columnHeaderSet;
	var cellSet = crossTable.cellSet;
	
	var width = cHeaderSet[0].length + rHeaderSet[0].length;
	var height = cHeaderSet.length + rHeaderSet.length;
	var leftPadding = rHeaderSet[0].length;
	var topPadding = cHeaderSet.length;
	
	var tabNode = document.createElement("table");
	tabNode.style.border = "1";
	for (var i = 0; i < height; i ++ ) {
		var trNode = document.createElement("tr");
		tabNode.appendChild(trNode);
		for (var j = 0; j < width; j++) {
			var tdNode = document.createElement("td");
			trNode.appendChild(tdNode);
			
			if (j >= leftPadding) {
				if (i >= topPadding)
					tdNode.innerHtml = cellSet[i - topPadding][j - leftPadding];
				else
					tdNode.innerHtml = cHeaderSet[i][j - leftPadding];
			} else {
				if (i >= topPadding)
					tdNode.innerHtml = cellSet[i - topPadding][j];
				else
					tdNode.innerHtml = "";
			}
				
		}
	}
	document.body.appendChild(tabNode);
}

var switchAs = function() {
	document.getElementById("request").disabled = false;
	document.getElementById("asFunc").disabled = false;
	document.getElementById("precision").disabled = false;
	document.getElementById("sendAs").disabled = false;
	
	document.getElementById("path").disabled = true;
	document.getElementById("rsFunc").disabled = true;
	document.getElementById("param1").disabled = true;
	document.getElementById("param2").disabled = true;
	document.getElementById("param3").disabled = true;
	document.getElementById("param4").disabled = true;
	document.getElementById("param5").disabled = true;
	document.getElementById("sendRs").disabled = true;
}

var switchRs = function() {
	document.getElementById("request").disabled = true;
	document.getElementById("asFunc").disabled = true;
	document.getElementById("precision").disabled = true;
	document.getElementById("sendAs").disabled = true;
	
	document.getElementById("path").disabled = false;
	document.getElementById("rsFunc").disabled = false;
	document.getElementById("param1").disabled = false;
	document.getElementById("param2").disabled = false;
	document.getElementById("param3").disabled = false;
	document.getElementById("param4").disabled = false;
	document.getElementById("param5").disabled = false;
	document.getElementById("sendRs").disabled = false;
}
</script>

<body onload="javascript:document.getElementById('as').checked=true;switchAs();">
<form action="QueryEditorServlet" method="post">
<input type="radio" name="type" id="as" value="as" onclick="javascript:switchAs();">Analysis Services&nbsp;&nbsp;&nbsp;&nbsp;
Function : 
<select name="asFunc" id="asFunc">
	<option value="array">Display result as array</option>
	<option value="csv">Export result to CSV</option>
	<option value="xsl">Export result to Excel</option>
	<option value="xml">Export result to XML</option>
</select>&nbsp;&nbsp;&nbsp;&nbsp;
Data Precision : <input type="text" name="precision" id="precision" value="2">
<br/>
MDX Query :<br/>
<textarea rows="20" cols="150" id="request" name="request"></textarea><br/>
<input type="button" value="Send" id="sendAs" onclick="javascript:send();"/><br/>
<br/>
<hr/>
<br/>
<input type="radio" name="type" id="rs" value="rs" onclick="javascript:switchRs();">Reporting Services&nbsp;&nbsp;&nbsp;&nbsp;
Function : 
<select name="rsFunc" id="rsFunc">
	<option value="html4.0">Display result as HTML</option>
	<option value="excel">Export result to Excel</option>
</select>
<br/>
Report : 
<select name="path" id="path">
	<option value="ANALYSIS_TABLE">Category Analysis Table</option>
</select><br/>
Report Parameter 1 : <input type="text" name="param1" id="param1" maxlength="40" size="40">(Eg. catFilter=Bikes)<br/>
Report Parameter 2 : <input type="text" name="param2" id="param2" maxlength="40" size="40">(Eg. subCatFilter=Road*)<br/>
Report Parameter 3 : <input type="text" name="param3" id="param3" maxlength="40" size="40"><br/>
Report Parameter 4 : <input type="text" name="param4" id="param4" maxlength="40" size="40"><br/>
Report Parameter 5 : <input type="text" name="param5" id="param5" maxlength="40" size="40"><br/>
<input type="button" value="Send" id="sendRs" onclick="javascript:send();"/>
</form>
</body>
</html>